package com.example.dell.fastshopping.views;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.network.MyCallBack;

import java.lang.ref.WeakReference;

/**
 * Created by ${YSH} on 2019/4/18.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class InputCode extends RelativeLayout implements View.OnClickListener {

    private MyCallBack callBack;
    private TextView coedeView;
    private MyHandler myHandler;
    private MyEditText code;


    public InputCode(Context context) {
        super(context);
        initView(context);
    }


    public InputCode(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public InputCode(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        myHandler = new MyHandler(this);
        RelativeLayout view = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.layout_code, this, false);
        coedeView = view.findViewById(R.id.button_code);
        code = view.findViewById(R.id.code);
        coedeView.setOnClickListener(this);
        addView(view);
    }

    @Override
    public void onClick(View v) {
        if (callBack != null) {
            callBack.callback();
        }
    }

    public String getText() {
        return code.getText().toString();
    }

    public void start() {
        Message obtain = Message.obtain();
        obtain.arg1 = 60;
        myHandler.sendMessageDelayed(obtain, 500);
    }

    public void setCallBack(MyCallBack callBack) {
        this.callBack = callBack;
    }

    public void setText(String s) {
        code.setText(s);
    }

    public void close() {
        code.setText("");
        myHandler.removeCallbacks(null);
    }

    private static class MyHandler extends Handler {
        private WeakReference<InputCode> inputView;

        public MyHandler(InputCode input) {
            inputView = new WeakReference<>(input);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            InputCode inputCode = inputView.get();
            if (inputCode != null) {
                int v = msg.arg1;
                if (v > 0) {
                    inputCode.coedeView.setText(v + " s");
                    Message obtain = Message.obtain();
                    obtain.arg1 = --v;
                    inputCode.myHandler.sendMessageDelayed(obtain, 1000);
                } else {
                    inputCode.coedeView.setText("获取验证码");
                }
            }
        }
    }
}
